# 白话人工智能算法实战主页面
# GUI库
from tkinter import *
from tkinter import filedialog

# 日期库
import datetime

# 创建一个窗口对象
win = Tk()
# 窗口标题
win.title("白话人工智能算法实战")
# 窗口图标
win.iconbitmap("img/logo.ico")
# 窗口大小("长x宽+X坐标+Y坐标")
win.geometry("1024x768")
# 背景颜色
win.configure(bg="#333")
# 精确显示本地时钟
dstr = StringVar()
# 获取日期时间
def get_time():
    # 获取当前时间
    dstr.set(datetime.datetime.now())
    win.after(1000, get_time)

# 状态栏
status = Label(win, textvariable=dstr, fg="#FFF", bg="#F90", font=("微软雅黑", 12))
status.pack(side=BOTTOM, fill="x")
# 调用时钟函数
get_time()

IDE = Text(win,width=50, height=14, bg="#CCB")
IDE.pack(fill=BOTH, expand=True)

# 读取文件到文本框中
def fileRead(string):
    dstr.set("打开文件：" + string)
    with open(string, 'r', encoding="UTF-8") as file:
        code = file.read()
        IDE.delete(1.0,END)
        IDE.insert(END,code)
        return code


# 加载源码文件路径
def codeOpen(string):
    code = fileRead(string)
    exec(code)

# 运行代码
def codeRun():
    code = IDE.get(1.0,END)
    exec(code)


# 打开本地文件
def fileOpen():
    file_path = filedialog.askopenfilename(
        title="选择文件",
        filetypes=(("Python文件", "*.py"), ("所有文件", "*.*"))
    )
    if file_path:
        # 读取文件内容
        try:
            fileRead(file_path)
        except Exception as e:
            dstr.set("读取文件时出错：" + e)


# 保存文件
def fileSave():
    file_path = filedialog.asksaveasfilename(
        defaultextension=".py",
        filetypes=(("Python文件", "*.py"), ("所有文件", "*.*"))
    )
    if file_path:
        dstr.set("保存文件：" + file_path)
        try:
            # 保存文件
            code = IDE.get(1.0,END)
            with open(file_path, 'w',encoding="UTF-8") as file:
                file.write(code)
        except Exception as e:
            dstr.set("保存文件时出错：" + e)


# 创建主菜单
mainMenu = Menu(win)

# 专家系统
menuES = Menu(mainMenu, tearoff=0)
mainMenu.add_cascade(label="专家系统", menu=menuES)
menu_exe = Menu(menuES, tearoff=0)
menuES.add_cascade(label="应用程序", menu=menu_exe)
menu_exe.add_command(label="简单计算器", command=lambda: codeOpen("1/1/computer.py"))
menu_exe.add_command(label="生成JS代码", command=lambda: codeOpen("1/1/js.py"))
menu_exe.add_command(label="误差的处理", command=lambda: codeOpen("1/1/float.py"))
menu_exe.add_command(label="汉语言编程", command=lambda: codeOpen("1/1/cn.py"))
menu_exe.add_command(label="音乐播放器", command=lambda: codeOpen("1/1/music.py"))
menu_qa = Menu(menuES, tearoff=0)
menuES.add_cascade(label="问答系统", menu=menu_qa)
menu_qa.add_command(label="语法机器人1", command=lambda: codeOpen("1/2/chinese.py"))
menu_qa.add_command(label="语法机器人2", command=lambda: codeOpen("1/2/chinese2.py"))
menu_qa.add_command(label="电商客服机器人", command=lambda: codeOpen("1/2/cs.py"))
menu_qa.add_command(label="贷款评估机器人", command=lambda: codeOpen("1/2/sale.py"))
menu_qa.add_command(label="话术管理系统", command=lambda: codeOpen("1/2/talks.py"))
menu_doctor = Menu(menuES, tearoff=0)
menuES.add_cascade(label="医疗专家", menu=menu_doctor)
menu_doctor.add_command(label="流感诊断机器人", command=lambda: codeOpen("1/3/doctor.py"))
menu_doctor.add_command(label="中医诊断机器人", command=lambda: codeOpen("1/3/doctor2.py"))
menu_doctor.add_command(label="术后康复机器人", command=lambda: codeOpen("1/3/doctor3.py"))
menu_doctor.add_command(label="心理康复机器人", command=lambda: codeOpen("1/3/doctor3.py"))
menu_data = Menu(menuES, tearoff=0)
menuES.add_cascade(label="数据存储", menu=menu_data)
menu_data.add_command(label="知识图谱", command=lambda: codeOpen("1/4/kg.py"))
menu_data.add_command(label="关系型数据库", command=lambda: codeOpen("1/4/web_sql.py"))
menu_data.add_command(label="索引型数据库", command=lambda: codeOpen("1/4/indexed_db.py"))
menu_data.add_command(label="节点型数据库", command=lambda: codeOpen("1/4/node_db.py"))

# 统计分析
menuST = Menu(mainMenu, tearoff=0)
mainMenu.add_cascade(label="统计分析", menu=menuST)
menu_stat = Menu(menuST, tearoff=0)
menuST.add_cascade(label="简单统计", menu=menu_stat)
menu_stat.add_command(label="预测命中率", command=lambda: codeOpen("2/1/shoot.py"))
menu_stat.add_command(label="预测打哪里", command=lambda: codeOpen("2/1/shoot2.py"))
menu_stat.add_command(label="自动分等级", command=lambda: codeOpen("2/1/shoot3.py"))
menu_stat.add_command(label="计算稳定性", command=lambda: codeOpen("2/1/shoot4.py"))
menu_similar = Menu(menuST, tearoff=0)
menuST.add_cascade(label="推荐算法", menu=menu_similar)
menu_similar.add_command(label="统计分词", command=lambda: codeOpen("2/2/words.py"))
menu_similar.add_command(label="语法分词", command=lambda: codeOpen("2/2/words2.py"))
menu_similar.add_command(label="相似推荐", command=lambda: codeOpen("2/2/similar.py"))
menu_similar.add_command(label="协同推荐", command=lambda: codeOpen("2/2/similar2.py"))
menu_bayes = Menu(menuST, tearoff=0)
menuST.add_cascade(label="先验概率", menu=menu_bayes)
menu_similar.add_command(label="贝叶斯定理", command=lambda: codeOpen("2/3/bayes.py"))
menu_similar.add_command(label="朴素贝叶斯", command=lambda: codeOpen("2/3/bayes_naive.py"))
menu_similar.add_command(label="预测股票走势", command=lambda: codeOpen("2/3/quant.py"))
menu_sort = Menu(menuST, tearoff=0)
menuST.add_cascade(label="排序算法", menu=menu_sort)
menu_sort.add_command(label="排序方法", command=lambda: codeOpen("2/4/sort.py"))
menu_sort.add_command(label="冒泡排序", command=lambda: codeOpen("2/4/bubble.py"))
menu_sort.add_command(label="欲望的提升", command=lambda: codeOpen("2/4/bubble_need.py"))
menu_sort.add_command(label="插入排序", command=lambda: codeOpen("2/4/insertion.py"))
menu_sort.add_command(label="选择排序", command=lambda: codeOpen("2/4/selection.py"))
menu_sort.add_command(label="快速排序", command=lambda: codeOpen("2/4/quick.py"))
menu_sort.add_command(label="细胞的分裂", command=lambda: codeOpen("2/4/quick_cell.py"))
menu_sort.add_command(label="归并排序", command=lambda: codeOpen("2/4/merge.py"))
menu_sort.add_command(label="希尔排序", command=lambda: codeOpen("2/4/shell.py"))
menu_sort.add_command(label="堆排序", command=lambda: codeOpen("2/4/heap.py"))
menu_sort.add_command(label="计数排序", command=lambda: codeOpen("2/4/array_count.py"))
menu_sort.add_command(label="模拟感受器", command=lambda: codeOpen("2/4/array_img.py"))
menu_sort.add_command(label="基数排序", command=lambda: codeOpen("2/4/array_radix.py"))
menu_sort.add_command(label="桶排序", command=lambda: codeOpen("2/4/array_bucket.py"))
menu_sort.add_command(label="休眠排序", command=lambda: codeOpen("2/4/array_sleep.py"))

# 机器学习
menuML = Menu(mainMenu, tearoff=0)
mainMenu.add_cascade(label="机器学习", menu=menuML)
menu_font = Menu(menuES, tearoff=0)
menuML.add_cascade(label="识别文字", menu=menu_font)
menu_font.add_command(label="简单计算器", command=lambda: codeOpen("3/1/font.py"))
menu_font.add_command(label="生成JS代码", command=lambda: codeOpen("3/1/font_1.py"))
menu_font.add_command(label="误差的处理", command=lambda: codeOpen("3/1/font_2.py"))
menu_font.add_command(label="汉语言编程", command=lambda: codeOpen("3/1/font_3.py"))
menu_font.add_command(label="音乐播放器", command=lambda: codeOpen("3/1/font_4.py"))
menu_teacher = Menu(menuML, tearoff=0)
menuML.add_cascade(label="监督学习", menu=menu_teacher)
menu_teacher.add_command(label="语法机器人1", command=lambda: codeOpen("3/2/font_img.py"))
menu_teacher.add_command(label="语法机器人2", command=lambda: codeOpen("3/2/nn.py"))
menu_teacher.add_command(label="电商客服机器人", command=lambda: codeOpen("3/2/nn_line.py"))
menu_teacher.add_command(label="贷款评估机器人", command=lambda: codeOpen("3/2/nn_linear.py"))
menu_teacher.add_command(label="话术管理系统", command=lambda: codeOpen("3/2/nn_bp.py"))
menu_deep = Menu(menuML, tearoff=0)
menuML.add_cascade(label="深度学习", menu=menu_deep)
menu_deep.add_command(label="流感诊断机器人", command=lambda: codeOpen("3/3/nn_cnn.py"))
menu_deep.add_command(label="中医诊断机器人", command=lambda: codeOpen("3/3/nn_rnn.py"))
menu_deep.add_command(label="术后康复机器人", command=lambda: codeOpen("3/3/nn_nlp.py"))
menu_deep.add_command(label="心理康复机器人", command=lambda: codeOpen("3/3/nn_chat.py"))
menu_tf = Menu(menuML, tearoff=0)
menuML.add_cascade(label="学习框架", menu=menu_tf)
menu_tf.add_command(label="知识图谱", command=lambda: codeOpen("3/4/tf.py"))
menu_tf.add_command(label="关系型数据库", command=lambda: codeOpen("3/4/tf_linear.py"))
menu_tf.add_command(label="索引型数据库", command=lambda: codeOpen("3/4/tf_mnist.py"))
menu_tf.add_command(label="节点型数据库", command=lambda: codeOpen("3/4/tf_gan.py"))

# 智能机器
menuAI = Menu(mainMenu, tearoff=0)
mainMenu.add_cascade(label="智能机器", menu=menuAI)
menu_path = Menu(menuES, tearoff=0)
menuAI.add_cascade(label="路径规划", menu=menu_path)
menu_path.add_command(label="绘制地图", command=lambda: codeOpen("4/1/map.py"))
menu_path.add_command(label="独立计算", command=lambda: codeOpen("4/1/self.py"))
menu_path.add_command(label="优先算法", command=lambda: codeOpen("4/1/first.py"))
menu_path.add_command(label="蚁群算法", command=lambda: codeOpen("4/1/ant.py"))
menu_path.add_command(label="蜂群算法", command=lambda: codeOpen("4/1/bee.py"))
menu_dna = Menu(menuAI, tearoff=0)
menuAI.add_cascade(label="遗传算法", menu=menu_dna)
menu_dna.add_command(label="二进制遗传", command=lambda: codeOpen("4/2/bit.py"))
menu_dna.add_command(label="旅行商问题", command=lambda: codeOpen("4/2/np.py"))
menu_dna.add_command(label="竞争与合作", command=lambda: codeOpen("4/2/group.py"))
menu_dna.add_command(label="贡献值优先", command=lambda: codeOpen("4/2/help.py"))
menu_dna.add_command(label="开放式地图", command=lambda: codeOpen("4/2/open_map.py"))
menu_coding = Menu(menuAI, tearoff=0)
menuAI.add_cascade(label="自我编程", menu=menu_coding)
menu_coding.add_command(label="编程引擎", command=lambda: codeOpen("4/3/code_js.py"))
menu_coding.add_command(label="语法词典", command=lambda: codeOpen("4/3/code_dict.py"))
menu_coding.add_command(label="进化编程", command=lambda: codeOpen("4/3/code_reg.py"))
menu_coding.add_command(label="迁移学习", command=lambda: codeOpen("4/3/code_git.py"))
menu_computer = Menu(menuAI, tearoff=0)
menuAI.add_cascade(label="人机结合", menu=menu_computer)
menu_computer.add_command(label="碳基生命", command=lambda: codeOpen("4/4/people.py"))
menu_computer.add_command(label="硅基生命", command=lambda: codeOpen("4/4/computer.py"))
menu_computer.add_command(label="脑机接口", command=lambda: codeOpen("4/4/api.py"))
menu_computer.add_command(label="超级生命", command=lambda: codeOpen("4/4/super.py"))

# 算法优化
menuAL = Menu(mainMenu, tearoff=0)
mainMenu.add_cascade(label="算法优化", menu=menuAL)
menu_arg = Menu(menuAL, tearoff=0)
menuAL.add_cascade(label="数据优化", menu=menu_arg)
menu_arg.add_command(label="样本选择", command=lambda: codeOpen("5/1/select.py"))
menu_arg.add_command(label="降维算法", command=lambda: codeOpen("5/1/pca.py"))
menu_arg.add_command(label="缩小图片", command=lambda: codeOpen("5/1/zoom.py"))
menu_arg.add_command(label="归一算法", command=lambda: codeOpen("5/1/one.py"))
menu_arg.add_command(label="聚类算法", command=lambda: codeOpen("5/1/class.py"))
menu_serch = Menu(menuAL, tearoff=0)
menuAL.add_cascade(label="查找算法", menu=menu_serch)
menu_serch.add_command(label="逐行查找", command=lambda: codeOpen("5/2/search.py"))
menu_serch.add_command(label="散列查找", command=lambda: codeOpen("5/2/search_hash.py"))
menu_serch.add_command(label="二分查找", command=lambda: codeOpen("5/2/search_binary.py"))
menu_serch.add_command(label="插值查找", command=lambda: codeOpen("5/2/search_inter.py"))
menu_serch.add_command(label="树形查找", command=lambda: codeOpen("5/2/search_tree.py"))
menu_rl = Menu(menuAL, tearoff=0)
menuAL.add_cascade(label="强化学习", menu=menu_rl)
menu_rl.add_command(label="博弈算法", command=lambda: codeOpen("5/3/max_min.py"))
menu_rl.add_command(label="剪枝搜索", command=lambda: codeOpen("5/3/alpha_beta.py"))
menu_rl.add_command(label="概率模型", command=lambda: codeOpen("5/3/monte_carlo.py"))
menu_rl.add_command(label="强化学习", command=lambda: codeOpen("5/3/rl.py"))
menu_concurrent = Menu(menuAL, tearoff=0)
menuAL.add_cascade(label="并行计算", menu=menu_concurrent)
menu_concurrent.add_command(label="缓存和队列", command=lambda: codeOpen("5/4/cache.py"))
menu_concurrent.add_command(label="线程和进程", command=lambda: codeOpen("5/4/worker.py"))
menu_concurrent.add_command(label="GPU编程", command=lambda: codeOpen("5/4/gpu.py"))
menu_concurrent.add_command(label="NODE.JS", command=lambda: codeOpen("5/4/node.py"))

# 帮助菜单
mainMenu.add_command(label="附录", command=lambda: fileRead("README.md"))

# 工具菜单
menuIDE = Menu(mainMenu,tearoff=0)
mainMenu.add_cascade(label="工具",menu=menuIDE)
menuIDE.config(bg="#09F",fg="#EEE")
menuIDE.add_command(label="运行🚩", command=lambda: codeRun())
menuIDE.add_command(label="打开📂", command=lambda: fileOpen())
menuIDE.add_command(label="保存💾", command=lambda: fileSave())
# 显示菜单
win.config(menu=mainMenu)

win.mainloop()
